//https://leetcode.cn/problems/last-stone-weight-ii/
package codeRandomThoughts.Test1049最后一块石头的重量II;

public class Solution2 {
    public int lastStoneWeightII(int[] stones) {
        int sum = 0;
        for (int stone : stones) {
            sum += stone;
        }

        int[] dp = new int[sum / 2 + 1];

        //初始化
        for (int j = stones[0]; j < dp.length; j++) {
            dp[j] = stones[0];
        }

        for (int i = 1; i < stones.length; i++) {
            for (int j = dp.length - 1; j >= stones[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }

        return Math.abs((sum - dp[dp.length - 1]) - dp[dp.length - 1]);
    }
}
